package com.sum.tea.xinlian.manager;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.sum.tea.R;
import com.sum.tea.contants.AppConfig;
import com.uhf.api.cls.Reader;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MainManager {
    /**
     * 根据android 平台设置显示语言
     */
    public static void setLange(Context context) {
        Locale locale = context.getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        AppConfig.Constr_READ = context.getString(R.string.Constr_READ);
        AppConfig.Constr_CONNECT = context.getString(R.string.Constr_CONNECT);
        AppConfig.Constr_INVENTORY = context.getString(R.string.Constr_INVENTORY);
        AppConfig.Constr_RWLOP = context.getString(R.string.Constr_RWLOP);
        AppConfig.Constr_set = context.getString(R.string.Constr_set);
        AppConfig.Constr_SetFaill = context
                .getString(R.string.Constr_SetFaill);
        AppConfig.Constr_GetFaill = context
                .getString(R.string.Constr_GetFaill);
        AppConfig.Constr_SetOk = context.getString(R.string.Constr_SetOk);
        AppConfig.Constr_unsupport = context
                .getString(R.string.Constr_unsupport);
        AppConfig.Constr_Putandexit = context
                .getString(R.string.Constr_Putandexit);

        AppConfig.Constr_stopscan = context
                .getString(R.string.Constr_stopscan);
        AppConfig.Constr_hadconnected = context
                .getString(R.string.Constr_hadconnected);
        AppConfig.Constr_plsetuuid = context
                .getString(R.string.Constr_plsetuuid);
        AppConfig.Constr_pwderror = context
                .getString(R.string.Constr_pwderror);
        AppConfig.Constr_search = context.getString(R.string.Constr_search);
        AppConfig.Constr_stop = context.getString(R.string.Constr_stop);

        AppConfig.Constr_createreaderok = context
                .getString(R.string.Constr_createreaderok);

        AppConfig.Constr_sub3readmem = context
                .getString(R.string.Constr_sub3readmem);
        AppConfig.Constr_sub3writemem = context
                .getString(R.string.Constr_sub3writemem);
        AppConfig.Constr_sub3lockkill = context
                .getString(R.string.Constr_sub3lockkill);
        AppConfig.Constr_sub3readfail = context
                .getString(R.string.Constr_sub3readfail);
        AppConfig.Constr_sub3nodata = context
                .getString(R.string.Constr_sub3nodata);
        AppConfig.Constr_sub3wrtieok = context
                .getString(R.string.Constr_sub3wrtieok);
        AppConfig.Constr_sub3writefail = context
                .getString(R.string.Constr_sub3writefail);
        AppConfig.Constr_sub3lockok = context
                .getString(R.string.Constr_sub3lockok);
        AppConfig.Constr_sub3lockfail = context
                .getString(R.string.Constr_sub3lockfail);
        AppConfig.Constr_sub3killok = context
                .getString(R.string.Constr_sub3killok);
        AppConfig.Constr_sub3killfial = context
                .getString(R.string.Constr_sub3killfial);

        AppConfig.Auto = context.getString(R.string.Auto);
        AppConfig.No = context.getString(R.string.No);
        AppConfig.Constr_sub4invenpra = context
                .getString(R.string.Constr_sub4invenpra);
        AppConfig.Constr_sub4antpow = context
                .getString(R.string.Constr_sub4antpow);
        AppConfig.Constr_sub4regionfre = context
                .getString(R.string.Constr_sub4regionfre);
        AppConfig.Constr_sub4gen2opt = context
                .getString(R.string.Constr_sub4gen2opt);
        AppConfig.Constr_sub4invenfil = context
                .getString(R.string.Constr_sub4invenfil);
        AppConfig.Constr_sub4addidata = context
                .getString(R.string.Constr_sub4addidata);
        AppConfig.Constr_sub4others = context
                .getString(R.string.Constr_sub4others);
        AppConfig.Constr_sub4quickly = context
                .getString(R.string.Constr_sub4quickly);
        AppConfig.Constr_sub4setmodefail = context
                .getString(R.string.Constr_sub4setmodefail);
        AppConfig.Constr_sub4setokresettoab = context
                .getString(R.string.Constr_sub4setokresettoab);
        AppConfig.Constr_sub4ndsapow = context
                .getString(R.string.Constr_sub4ndsapow);
        AppConfig.Constr_sub4unspreg = context
                .getString(R.string.Constr_sub4unspreg);

        AppConfig.Constr_subblmode = context
                .getString(R.string.Constr_subblmode);
        AppConfig.Constr_subblinven = context
                .getString(R.string.Constr_subblinven);
        AppConfig.Constr_subblfil = context
                .getString(R.string.Constr_subblfil);
        AppConfig.Constr_subblfre = context
                .getString(R.string.Constr_subblfre);
        AppConfig.Constr_subblnofre = context
                .getString(R.string.Constr_subblnofre);

        AppConfig.Constr_subcsalterpwd = context
                .getString(R.string.Constr_subcsalterpwd);
        AppConfig.Constr_subcslockwpwd = context
                .getString(R.string.Constr_subcslockwpwd);
        AppConfig.Constr_subcslockwoutpwd = context
                .getString(R.string.Constr_subcslockwoutpwd);
        AppConfig.Constr_subcsplsetimeou = context
                .getString(R.string.Constr_subcsplsetimeou);
        AppConfig.Constr_subcsputcnpwd = context
                .getString(R.string.Constr_subcsputcnpwd);
        AppConfig.Constr_subcsplselreg = context
                .getString(R.string.Constr_subcsplselreg);
        AppConfig.Constr_subcsopfail = context
                .getString(R.string.Constr_subcsopfail);
        AppConfig.Constr_subcsputcurpwd = context
                .getString(R.string.Constr_subcsputcurpwd);

        AppConfig.Constr_subdbdisconnreconn = context
                .getString(R.string.Constr_subdbdisconnreconn);
        AppConfig.Constr_subdbhadconnected = context
                .getString(R.string.Constr_subdbhadconnected);
        AppConfig.Constr_subdbconnecting = context
                .getString(R.string.Constr_subdbconnecting);
        AppConfig.Constr_subdbrev = context
                .getString(R.string.Constr_subdbrev);
        AppConfig.Constr_subdbstop = context
                .getString(R.string.Constr_subdbstop);
        AppConfig.Constr_subdbdalennot = context
                .getString(R.string.Constr_subdbdalennot);
        AppConfig.Constr_subdbplpuhexchar = context
                .getString(R.string.Constr_subdbplpuhexchar);

        AppConfig.Constr_subsysaveok = context
                .getString(R.string.Constr_subsysaveok);
        AppConfig.Constr_subsysout = context
                .getString(R.string.Constr_subsysout);
        AppConfig.Constr_subsysreavaid = context
                .getString(R.string.Constr_subsysreavaid);
        AppConfig.Constr_sub1recfailed = context
                .getString(R.string.Constr_sub1recfailed);
        AppConfig.Constr_subsysavefailed = context
                .getString(R.string.Constr_subsysavefailed);
        AppConfig.Constr_subsysexefin = context
                .getString(R.string.Constr_subsysexefin);
        AppConfig.Constr_sub1adrno = context
                .getString(R.string.Constr_sub1adrno);
        AppConfig.Constr_sub1pdtsl = context
                .getString(R.string.Constr_sub1pdtsl);
        AppConfig.Constr_mainpu = context.getString(R.string.Constr_mainpu);
        AppConfig.Constr_nostopstreadfailed = context
                .getString(R.string.Constr_nostopstreadfailed);
        AppConfig.Constr_nostopspreadfailed = context
                .getString(R.string.Constr_nostopspreadfailed);
        AppConfig.Constr_nostopreadfailed = context
                .getString(R.string.Constr_nostopreadfailed);
        AppConfig.Constr_connectok = context
                .getString(R.string.Constr_connectok);
        AppConfig.Constr_connectfialed = context
                .getString(R.string.Constr_connectfialed);
        AppConfig.Constr_disconpowdown = context
                .getString(R.string.Constr_disconpowdown);
        AppConfig.Constr_ok = context.getString(R.string.Constr_ok);
        AppConfig.Constr_failed = context.getString(R.string.Constr_failed);
        AppConfig.Constr_excep = context.getString(R.string.Constr_excep);
        AppConfig.Constr_setcep = context.getString(R.string.Constr_setcep);
        AppConfig.Constr_getcep = context.getString(R.string.Constr_getcep);
        AppConfig.Constr_killok = context.getString(R.string.Constr_killok);
        AppConfig.Constr_killfailed = context
                .getString(R.string.Constr_killfailed);
        AppConfig.Constr_psiant = context.getString(R.string.Constr_psiant);
        AppConfig.Constr_selpro = context.getString(R.string.Constr_selpro);

        AppConfig.Constr_carry_fftable = context
                .getString(R.string.Constr_carry_fftable);
        AppConfig.Constr_carry_frtable = context
                .getString(R.string.Constr_carry_frtable);
        AppConfig.Constr_carry_binvpw = context
                .getString(R.string.Constr_carry_binvpw);
        AppConfig.Constr_carry_binvfpw = context
                .getString(R.string.Constr_carry_binvfpw);
        AppConfig.Constr_carry_invc = context
                .getString(R.string.Constr_carry_invc);
        AppConfig.Constr_carry_invtep = context
                .getString(R.string.Constr_carry_invtep);
        AppConfig.Constr_carry_invpw = context
                .getString(R.string.Constr_carry_invpw);
        AppConfig.Constr_carry_ainvfpw = context
                .getString(R.string.Constr_carry_ainvfpw);
        AppConfig.Constr_carry_ainvpw = context
                .getString(R.string.Constr_carry_ainvpw);

        AppConfig.Coname = context.getResources().getStringArray(
                R.array.Coname);

        AppConfig.pdaatpot = context.getResources().getStringArray(
                R.array.pdaatpot);

        AppConfig.spibank = context.getResources().getStringArray(
                R.array.spibank);
        AppConfig.spifbank = context.getResources().getStringArray(
                R.array.spifbank);
        AppConfig.spilockbank = context.getResources().getStringArray(
                R.array.spilockbank);
        AppConfig.spilocktype = context.getResources().getStringArray(
                R.array.spilocktype);

        AppConfig.spireg = context.getResources().getStringArray(
                R.array.spireg);
        AppConfig.spitari = context.getResources().getStringArray(
                R.array.spitari);
        AppConfig.spiwmod = context.getResources().getStringArray(
                R.array.spiwmod);

        AppConfig.cusreadwrite = context.getResources().getStringArray(
                R.array.cusreadwrite);
        AppConfig.cuslockunlock = context.getResources().getStringArray(
                R.array.cuslockunlock);

        AppConfig.regtype = context.getResources().getStringArray(
                R.array.regtype);

        AppConfig.spiqmode = context.getResources().getStringArray(
                R.array.qmodes);
        AppConfig.gpodemo = context.getResources().getStringArray(
                R.array.gpodemo);

    }


    public static boolean reconnect(AppConfig myapp) {
        Log.d("MYINFO", "reconenct");
        boolean blen = myapp.Rpower.PowerUp();
        if (!blen){
            return false;
        }
        ToastUtils.showShort(AppConfig.Constr_mainpu + blen);

        Reader.READER_ERR er = myapp.Mreader.InitReader_Notype(myapp.Address, myapp.antportc);
        if (er == Reader.READER_ERR.MT_OK_ERR) {
            myapp.needreconnect = false;
            try {

                // myapp.Rparams = myapp.spf.ReadReaderParams();

                if (myapp.Rparams.invpro.size() < 1)
                    myapp.Rparams.invpro.add("GEN2");

                List<Reader.SL_TagProtocol> ltp = new ArrayList<Reader.SL_TagProtocol>();
                for (int i = 0; i < myapp.Rparams.invpro.size(); i++) {
                    if (myapp.Rparams.invpro.get(i).equals("GEN2")) {
                        ltp.add(Reader.SL_TagProtocol.SL_TAG_PROTOCOL_GEN2);

                    } else if (myapp.Rparams.invpro.get(i).equals("6B")) {
                        ltp.add(Reader.SL_TagProtocol.SL_TAG_PROTOCOL_ISO180006B);

                    } else if (myapp.Rparams.invpro.get(i).equals("IPX64")) {
                        ltp.add(Reader.SL_TagProtocol.SL_TAG_PROTOCOL_IPX64);

                    } else if (myapp.Rparams.invpro.get(i).equals("IPX256")) {
                        ltp.add(Reader.SL_TagProtocol.SL_TAG_PROTOCOL_IPX256);

                    }
                }

                Reader.Inv_Potls_ST ipst = myapp.Mreader.new Inv_Potls_ST();
                ipst.potlcnt = ltp.size();
                ipst.potls = new Reader.Inv_Potl[ipst.potlcnt];
                Reader.SL_TagProtocol[] stp = ltp
                        .toArray(new Reader.SL_TagProtocol[ipst.potlcnt]);
                for (int i = 0; i < ipst.potlcnt; i++) {
                    Reader.Inv_Potl ipl = myapp.Mreader.new Inv_Potl();
                    ipl.weight = 30;
                    ipl.potl = stp[i];
                    ipst.potls[0] = ipl;
                }

                er = myapp.Mreader.ParamSet(Reader.Mtr_Param.MTR_PARAM_TAG_INVPOTL,
                        ipst);
                Log.d("MYINFO", "Connected set pro:" + er.toString());

                er = myapp.Mreader.ParamSet(
                        Reader.Mtr_Param.MTR_PARAM_READER_IS_CHK_ANT,
                        new int[] { myapp.Rparams.checkant });
                Log.d("MYINFO", "Connected set checkant:" + er.toString());

                Reader.AntPowerConf apcf = myapp.Mreader.new AntPowerConf();
                apcf.antcnt = myapp.antportc;
                for (int i = 0; i < apcf.antcnt; i++) {
                    Reader.AntPower jaap = myapp.Mreader.new AntPower();
                    jaap.antid = i + 1;
                    jaap.readPower = (short) myapp.Rparams.rpow[i];
                    jaap.writePower = (short) myapp.Rparams.wpow[i];
                    apcf.Powers[i] = jaap;
                }

                myapp.Mreader.ParamSet(Reader.Mtr_Param.MTR_PARAM_RF_ANTPOWER, apcf);

                Reader.Region_Conf rre;
                switch (myapp.Rparams.region) {
                    case 0:
                        rre = Reader.Region_Conf.RG_PRC;
                        break;
                    case 1:
                        rre = Reader.Region_Conf.RG_NA;
                        break;
                    case 2:
                        rre = Reader.Region_Conf.RG_NONE;
                        break;
                    case 3:
                        rre = Reader.Region_Conf.RG_KR;
                        break;
                    case 4:
                        rre = Reader.Region_Conf.RG_EU;
                        break;
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    default:
                        rre = Reader.Region_Conf.RG_NONE;
                        break;
                }
                if (rre != Reader.Region_Conf.RG_NONE) {
                    er = myapp.Mreader.ParamSet(
                            Reader.Mtr_Param.MTR_PARAM_FREQUENCY_REGION, rre);
                }

                if (myapp.Rparams.frelen > 0) {

                    Reader.HoptableData_ST hdst = myapp.Mreader.new HoptableData_ST();
                    hdst.lenhtb = myapp.Rparams.frelen;
                    hdst.htb = myapp.Rparams.frecys;
                    er = myapp.Mreader.ParamSet(
                            Reader.Mtr_Param.MTR_PARAM_FREQUENCY_HOPTABLE, hdst);
                }

                er = myapp.Mreader.ParamSet(
                        Reader.Mtr_Param.MTR_PARAM_POTL_GEN2_SESSION,
                        new int[] { myapp.Rparams.session });
                er = myapp.Mreader.ParamSet(Reader.Mtr_Param.MTR_PARAM_POTL_GEN2_Q,
                        new int[] { myapp.Rparams.qv });
                er = myapp.Mreader.ParamSet(
                        Reader.Mtr_Param.MTR_PARAM_POTL_GEN2_WRITEMODE,
                        new int[] { myapp.Rparams.wmode });
                er = myapp.Mreader.ParamSet(
                        Reader.Mtr_Param.MTR_PARAM_POTL_GEN2_MAXEPCLEN,
                        new int[] { myapp.Rparams.maxlen });
                er = myapp.Mreader.ParamSet(
                        Reader.Mtr_Param.MTR_PARAM_POTL_GEN2_TARGET,
                        new int[] { myapp.Rparams.target });

                er = myapp.Mreader.ParamSet(
                        Reader.Mtr_Param.MTR_PARAM_TAGDATA_UNIQUEBYEMDDATA,
                        new int[] { myapp.Rparams.adataq });
                er = myapp.Mreader.ParamSet(
                        Reader.Mtr_Param.MTR_PARAM_TAGDATA_RECORDHIGHESTRSSI,
                        new int[] { myapp.Rparams.rhssi });
                er = myapp.Mreader.ParamSet(
                        Reader.Mtr_Param.MTR_PARAM_TAG_SEARCH_MODE,
                        new int[] { myapp.Rparams.invw });

                Reader.NXP_U8_InventoryModePara u8para = myapp.Mreader.new NXP_U8_InventoryModePara();

                u8para.Mode[0] = 0;

                if (myapp.nxpu8 == 0) {
                    if (myapp.Rparams.filenable == 1) {
                        Reader.TagFilter_ST tfst = myapp.Mreader.new TagFilter_ST();
                        tfst.bank = myapp.Rparams.filbank;
                        int len = myapp.Rparams.fildata.length();
                        len = len % 2 == 0 ? len : len + 1;
                        tfst.fdata = new byte[len / 2];
                        myapp.Mreader.Str2Hex(myapp.Rparams.fildata,
                                myapp.Rparams.fildata.length(), tfst.fdata);
                        tfst.flen = myapp.Rparams.fildata.length() * 4;
                        tfst.startaddr = myapp.Rparams.filadr;
                        tfst.isInvert = myapp.Rparams.filisinver;

                        myapp.Mreader.ParamSet(Reader.Mtr_Param.MTR_PARAM_TAG_FILTER,
                                tfst);
                    }

                    if (myapp.Rparams.emdenable == 1) {
                        Reader.EmbededData_ST edst = myapp.Mreader.new EmbededData_ST();

                        edst.accesspwd = null;
                        edst.bank = myapp.Rparams.emdbank;
                        edst.startaddr = myapp.Rparams.emdadr;
                        edst.bytecnt = myapp.Rparams.emdbytec;

                        er = myapp.Mreader.ParamSet(
                                Reader.Mtr_Param.MTR_PARAM_TAG_EMBEDEDDATA, edst);
                    }
                } else if (myapp.nxpu8 == 1) {
                    Reader.TagFilter_ST tfst = myapp.Mreader.new TagFilter_ST();

                    tfst.fdata = new byte[1];
                    tfst.fdata[0] = (byte) 0x80;
                    tfst.bank = 1;
                    tfst.flen = 1;
                    tfst.startaddr = 0x204;
                    tfst.isInvert = 0;
                    er = myapp.Mreader.ParamSet(Reader.Mtr_Param.MTR_PARAM_TAG_FILTER,
                            tfst);

                    Reader.EmbededData_ST edst = myapp.Mreader.new EmbededData_ST();
                    edst.accesspwd = null;

                    edst.bank = 2;
                    edst.startaddr = 0;
                    edst.bytecnt = 12;

                    er = myapp.Mreader.ParamSet(
                            Reader.Mtr_Param.MTR_PARAM_TAG_EMBEDEDDATA, edst);
                    if (er == Reader.READER_ERR.MT_OK_ERR) {
                    }

                    u8para.Mode[0] = 1;
                    myapp.nxpu8 = 1;
                } else if (myapp.nxpu8 == 2) {
                    Reader.TagFilter_ST tfst = myapp.Mreader.new TagFilter_ST();

                    tfst.fdata = new byte[1];
                    tfst.fdata[0] = (byte) 0x80;
                    tfst.bank = 1;
                    tfst.flen = 1;
                    tfst.startaddr = 0x203;
                    tfst.isInvert = 0;
                    er = myapp.Mreader.ParamSet(Reader.Mtr_Param.MTR_PARAM_TAG_FILTER,
                            tfst);

                    u8para.Mode[0] = 1;
                    myapp.nxpu8 = 2;
                } else if (myapp.nxpu8 == 3) {
                    Reader.TagFilter_ST tfst = myapp.Mreader.new TagFilter_ST();

                    tfst.fdata = new byte[1];
                    tfst.fdata[0] = (byte) 0x80;
                    tfst.bank = 1;
                    tfst.flen = 1;
                    tfst.startaddr = 0x204;
                    tfst.isInvert = 0;
                    er = myapp.Mreader.ParamSet(Reader.Mtr_Param.MTR_PARAM_TAG_FILTER,
                            tfst);
                    u8para.Mode[0] = 1;
                    myapp.nxpu8 = 3;
                }
                myapp.Mreader.CustomCmd(0, Reader.CustomCmdType.NXP_U8_InventoryMode,
                        u8para, null);

            } catch (Exception ex) {
                Log.d("MYINFO",
                        ex.getMessage() + ex.toString() + ex.getStackTrace());
            }
        } else
            return false;

        return true;
    }


    public static void showPopupMenu(Context context, View view, AppConfig config, List<String> Gpodemoauthortags) {
        // View当前PopupMenu显示的相对View的位置
        PopupMenu popupMenu = new PopupMenu(context, view);

        // menu布局
        popupMenu.getMenuInflater()
                .inflate(R.menu.tagspop, popupMenu.getMenu());
        // menu的item点击事件
        popupMenu
                .setOnMenuItemClickListener(item -> {

                    if (item.getItemId() == R.id.menu_tagop_add) {
                        if (!Gpodemoauthortags.contains(config.Curepc))
                            Gpodemoauthortags.add(config.Curepc);

                    } else if (item.getItemId() == R.id.menu_tagop_rem) {
                        if (Gpodemoauthortags.contains(config.Curepc))
                            Gpodemoauthortags.remove(config.Curepc);
                    }

                    Toast.makeText(context, item.getTitle(), Toast.LENGTH_SHORT).show();
                    return false;
                });
        // PopupMenu关闭事件
        popupMenu.setOnDismissListener(menu -> {

        });

        popupMenu.show();
    }
}
